package org.sninwo.lnbook.uitls;

import java.security.MessageDigest;

public class LangUtils {
	/**
	 * md5加密
	 */
	public static String md5(String src){
		if (src == null)
			return null;
		try{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(src.getBytes());
			StringBuilder dist = new StringBuilder();
			for (byte b : md.digest()){
				String s = Integer.toHexString(0xFF & b);
				if (s.length() == 1) dist.append("0");
				dist.append(s);
			}
			return dist.toString();
		}
		catch (Exception e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 去掉前后空白字符，包括中文空格
	 */
	public static String trimCn(String str){
		if (str == null)
			return null;
		str = str.trim();
		if (str.length() == 0)
			return str;
		int st = 0;
		int len = str.length();
		while((st < len) && str.charAt(st) == '　')
			st ++;
		while((st < len) && str.charAt(len - 1) == '　')
			len --;
		if (st > 0 || len < str.length())
			str = str.substring(st, len);
		return str.trim();
	}
	
	public static boolean contains(Object[] array, Object item){
		for (Object obj : array){
			if (obj.equals(item)){
				return true;
			}
		}
		return false;
	}
	
	public static String isoToUtf8(String str){
		if (str == null)
			return null;
		try{
			return new String(str.getBytes("iso_8859_1"), "utf-8");
		}
		catch (Exception e){
			throw new RuntimeException(e);
		}
	}

}
